
package com.catsic.zcjc.specifiction;

import javax.persistence.criteria.Path;

import org.apache.commons.lang3.StringUtils;

import com.catsic.base.specification.BaseSpec;
import com.catsic.base.specification.QueryWraper;
import com.catsic.zcjc.condition.EquipDataCondition;
import com.catsic.zcjc.entity.EquipData;

public class EquipDataSpec extends BaseSpec<EquipData> {

	public EquipDataSpec(EquipDataCondition condition){
		super(condition);
	}
	

	@Override
	protected void addCondition(QueryWraper<EquipData> queryWraper) {
		EquipDataCondition condition = (EquipDataCondition) getCondition();
		
		
		/**
		 * 多表联查,多对一,机构
		 */
		String stationId = condition.getStationId();
		if(StringUtils.isNotBlank(stationId)){
			Path<EquipData> station = queryWraper.getRoot().get("station");
			addEqualCondition(queryWraper, station.get("id"),stationId);
		}
		
		String equipId = condition.getEquipId();
		if(StringUtils.isNotBlank(equipId)){
			Path<EquipData> equip = queryWraper.getRoot().get("equip");
			addEqualCondition(queryWraper, equip.get("id"),equipId);
		}
		
		String beginDate = condition.getBeginDate();
		if(StringUtils.isNotBlank(beginDate)){
			Path<EquipData> beginDatePath = queryWraper.getRoot().get("collectionTime");
			queryWraper.getPredicates().add(queryWraper.getCb().greaterThanOrEqualTo(beginDatePath.as(String.class), beginDate));
		}
		
		String endDate = condition.getEndDate();
		if(StringUtils.isNotBlank(endDate)){
			Path<EquipData> endDatePath = queryWraper.getRoot().get("collectionTime");
			queryWraper.getPredicates().add(queryWraper.getCb().lessThanOrEqualTo(endDatePath.as(String.class), endDate));
		}
		
		Path<EquipData> plateNoPath = queryWraper.getRoot().get("plateNo");
		queryWraper.getPredicates().add(queryWraper.getCb().gt(queryWraper.getCb().length(plateNoPath.as(String.class)), 1));
	
		/**
		 * 自定义排序
		 */
		queryWraper.addOrder(queryWraper.getCb().desc(queryWraper.getRoot().get("collectionTime")));
		
		
	}
}
